package com.scrat.huzhenjie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.scrat.huzhenjie.model.Test;
import com.scrat.huzhenjie.model.Users;

@Service(value = "huzhenjieDao")
public class HuzhenjieDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public Users getUsers(String email){
		String sql = "SELECT user_id , email , password , username , birthday , registered_date , last_login FROM users WHERE email = ?";
		return jdbcTemplate.queryForObject(sql, new Object[]{ email } , new UsersMapper());
	}
	
	private class UsersMapper implements RowMapper<Users> {
		@Override
		public Users mapRow(ResultSet rs, int arg1) throws SQLException {
			Users users = new Users();
			users.setUserId(rs.getInt(""));
			users.setEmail(rs.getString(""));
			users.setPassword(rs.getString(""));
			users.setUsername(rs.getString(""));
			users.setBirthday(rs.getDate(""));
			users.setRegisteredDate(rs.getTimestamp(""));
			users.setLastLogin(rs.getTimestamp(""));
			return users;
		}
	}
	
	public Test testBAESQL(){
		String sql = "SELECT id , username , password FROM test";
		return jdbcTemplate.queryForObject(sql, new TestMapper());
	}
	
	private class TestMapper implements RowMapper<Test>{
		@Override
		public Test mapRow(ResultSet rs, int arg1) throws SQLException {
			Test t = new Test();
			t.setId(rs.getInt("id"));
			t.setUsername(rs.getString("username"));
			t.setPassword(rs.getString("password"));
			return t;
		}
	}
}
